home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE15 / CPPCLASS / cpp1632 / Cppclu.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-07-17  |  3.8 KB  |  156 lines

  1. unit Cppclu;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, ExtCtrls;
  8.  
  9. type
  10.  
  11.   { Mirror of the real C++ class - note the additional method attributes }
  12.   TCPPNotifyEvent = procedure (Sender: TObject) cdecl of object;
  13.   TDLLClass = class
  14.      FEvent   : TNotifyEvent;
  15.     procedure SetValue(Info : integer);      virtual; cdecl; abstract;
  16.     function  GetValue : integer;            virtual; cdecl; abstract;
  17.     procedure SetEvent(func : TCPPNotifyEvent); virtual; cdecl; abstract;
  18.     procedure ISetValue(Info : integer);
  19.     function  IGetValue : integer;
  20.     procedure ISetEvent(func : TNotifyEvent);
  21.     procedure FCPPNotifyEvent(Sender: TObject); cdecl; export;
  22.  
  23.   public
  24.     procedure ShowTheValue;   virtual; cdecl; abstract;
  25.     procedure DoEvent;        virtual; cdecl; abstract;
  26.  
  27.     property AnEvent : TNotifyEvent read FEvent  write ISetEvent;
  28.     property DLLValue: integer read IGetValue write ISetValue;
  29.   end;
  30.  
  31.   TForm1 = class(TForm)
  32.        Button1: TButton;
  33.        Button2: TButton;
  34.        Button3: TButton;
  35.        Button4: TButton;
  36.        Button5: TButton;
  37.        Button6: TButton;
  38.        procedure Button1Click(Sender: TObject);
  39.        procedure Button2Click(Sender: TObject);
  40.        procedure Button3Click(Sender: TObject);
  41.        procedure Button4Click(Sender: TObject);
  42.        procedure Button5Click(Sender: TObject);
  43.        procedure Button6Click(Sender: TObject);
  44.     private
  45.        procedure DisEnableAllButtons;
  46.     public
  47.        procedure bProc(Sender: TObject);
  48.        procedure cProc(Sender: TObject);
  49.     end;
  50.  
  51.  
  52. var
  53.   Form1: TForm1;
  54.   { Instance of C++ class created in C++ DLL }
  55.   ACppClass : TDLLClass;
  56.  
  57.  
  58. implementation
  59.  
  60. {$R *.DFM}
  61.  
  62. { Make a C++ class }
  63. function  _ConstructClass : TDllClass; cdecl; far;
  64.     external 'CPPDLL' name '_CONSTRUCTCLASS';
  65. procedure _DestructClass(DLLClass :TDllClass); cdecl; far;
  66.     external 'CPPDLL' name '_DESTRUCTCLASS';
  67.  
  68. procedure TDLLClass.ISetValue(Info : integer);
  69.  begin
  70.    SetValue(Info);
  71.  end;
  72.  
  73. function  TDLLClass.IGetValue : integer;
  74. begin
  75.   Result := GetValue;
  76. end;
  77.  
  78. procedure TDLLClass.ISetEvent(func : TNotifyEvent);
  79. begin
  80.     FEvent := func;
  81.     SetEvent(FCPPNotifyEvent);
  82. end;
  83.  
  84. procedure TDLLClass.FCPPNotifyEvent(Sender: TObject);
  85. begin
  86.   if Assigned(FEvent) then FEvent(Sender);
  87. end;
  88.  
  89. procedure TForm1.bProc(Sender: TObject);
  90. begin
  91.   Color   := clRed;
  92.   Caption := 'a GeneralEvent';
  93. end;
  94.  
  95. procedure TForm1.cProc(Sender: TObject);
  96. begin
  97.   Color   := clGreen;
  98.   Caption := 'another GeneralEvent';
  99. end;
  100.  
  101. procedure TForm1.Button1Click(Sender: TObject);
  102. begin
  103.   ACppClass := _ConstructClass;
  104.   if ACppClass <> nil then
  105.        DisEnableAllButtons;
  106. end;
  107.  
  108. procedure TForm1.Button2Click(Sender: TObject);
  109. begin
  110.   with ACppClass do
  111.   begin
  112.     { Call methods of C++ object }
  113.     ShowMessage(Format('C++ class Before: %d', [DLLValue]));
  114.     DLLValue := DLLValue + 10;
  115.     ShowMessage(Format('C++ class After: %d', [DLLValue]));
  116.     ShowThevalue;
  117.   end;
  118. end;
  119.  
  120. procedure TForm1.DisEnableAllButtons;
  121. var
  122.   i : integer;
  123. begin
  124.    {Flip all buttons to opposite state}
  125.    for i := 0 to ControlCount-1 do
  126.      if Controls[i] is TButton then
  127.        with TButton(Controls[i]) do
  128.            Enabled := not Enabled;
  129. end;
  130.  
  131. procedure TForm1.Button3Click(Sender: TObject);
  132. begin
  133.    ACppClass.AnEvent := bProc;
  134. end;
  135.  
  136. procedure TForm1.Button4Click(Sender: TObject);
  137. begin
  138.   ACppClass.AnEvent  := cProc;
  139. end;
  140.  
  141. procedure TForm1.Button5Click(Sender: TObject);
  142. begin
  143.   ACppClass.DoEvent;
  144. end;
  145.  
  146. procedure TForm1.Button6Click(Sender: TObject);
  147. begin
  148.    _DestructClass(ACppClass);
  149.    ACppClass := nil;
  150.     if ACppClass = nil then
  151.        DisEnableAllButtons;
  152.  
  153. end;
  154.  
  155. end.
  156.